package asapIs134CK.womi.client.games;

import java.util.Date;

import asapIs134CK.womi.client.event.RCPEventService;
import asapIs134CK.womi.client.event.RCPEventServiceAsync;
import asapIs134CK.womi.client.event.WomiEvent;
import asapIs134CK.womi.client.profile.RPCUserService;
import asapIs134CK.womi.client.profile.RPCUserServiceAsync;
import asapIs134CK.womi.client.profile.UserClient;
import asapIs134CK.womi.client.profile.UserDisplay;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;

/**
 * design to regroup the main widget for our different games
 * @author LeoJ
 *
 */
public abstract class GameWidget extends Composite{
	private UserClient _user; //user could be null
	private final RPCUserServiceAsync userService = GWT
	.create(RPCUserService.class);
	
	private final RCPEventServiceAsync eventService = GWT
	.create(RCPEventService.class);
	
	public GameWidget(){
		_user = null;
		AsyncCallback<UserClient> callback = new AsyncCallback<UserClient>() {
			public void onFailure(Throwable caught) {
				Window.alert("The server could not be reached, please check your network connection.");
			}

			public void onSuccess(UserClient result) {
				_user = result;
			}
		};
		userService.getCurrentUser(callback);
	}
	
	protected void submitScore(int score, GameType type){
		if(_user != null){
			switch(type){
				case Arithmetic :
					_user.setScoreArithmeticQuizz(_user.getScoreArithmeticQuizz()+score);
				break;

				case FroggitTF :
					_user.setScoreFroggit(_user.getScoreFroggit()+score);
				break;

				case Sudoku :
					_user.setScoreSudoku(_user.getScoreSudoku()+score);
				break;
				
				default : 
					// do nothing !
					break;
			}
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
				}

				public void onSuccess(Boolean res) {
					if(res){
						UserDisplay.getInstance().refresh();
					}
					else {
						Window.alert("Your score was not submitted...");
					}
				}
			};
			userService.modifyUser(_user, callback);
			String message = _user.getNickname()+" scored "+score+" point(s) "+" ";
			switch(type){
			case Arithmetic :
				message += " at Arithmetic Puzzle ! ";
				break;
			case FroggitTF :
				message += " at Froggit the frog ! ";
				break;
			case Sudoku :
				message += " at a Sudoku Puzzle ! ";
				break;
				
			}
			message += _user.getNickname()+"'s total score is now "+_user.getScore()+".";
			WomiEvent event = new WomiEvent(new Date(), message);
			AsyncCallback<Boolean> callback2 = new AsyncCallback<Boolean>(){

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(Boolean result) {
					// TODO Auto-generated method stub
					
				}};
			eventService.addEvent(event, callback2 );
		}else{
			DialogBox box = new DialogBox();
			box.setTitle("Login needed to submit score");
			box.add(new Label("sorry, you cannot submit your score if you are not logged in ..."));
		}
	}
}
